﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Fishing.Dal;
using Fishing.Model;

namespace Fishing.Admin
{
    public partial class User : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //绑定数据
                BindData();
                BindDropDown();
            }
        }

        public void BindDropDown()
        {
            var _dal = new AdminRoleDal();
            var alldata = _dal.GetRoleDataBy("");
            ddlRoles.Items.Clear();
            ddlRoles.DataSource = alldata;
            ddlRoles.DataTextField = "roleName";
            ddlRoles.DataValueField = "roleID";
            ddlRoles.DataBind();
        }

        public void BindData()
        {
            

            var _dal = new AdminRoleDal();
            var alldata = _dal.GetRoleDataBy("");

            tvMenu.Nodes.Clear();
            // 首先绑定子节点
            foreach (var adminRole in alldata)
            {
                TreeNode rootNode = new TreeNode();
                rootNode.Text = adminRole.roleName;
                rootNode.Value = adminRole.roleID;
                rootNode.Target = "角色";
                this.tvMenu.Nodes.Add(rootNode);
                BindUserData(adminRole.roleID, rootNode);
                //tvMenu.Nodes.Add(new TreeNode(adminRole.roleName, adminRole.roleID));
                //如果存在子节点
            }

            tvMenu.ExpandAll();
        }

        public void BindUserData(string roleID,TreeNode node)
        {
            var where = "";
            if (txtSerchWords.Text.Trim().Length > 0)
            {
                where = " and userName   like '%"+ txtSerchWords.Text.Trim() + "%'";
            }
            var _dal = new AdminUserRoleDal();
            var alldata = _dal.GetUserDataByRoleID(roleID, where);
            // 首先绑定子节点
            foreach (var adminUser in alldata)
            {
                node.ChildNodes.Add(new TreeNode(adminUser.userName, adminUser.userID));
            }
        }

        protected void ImgBtnCan_Click(object sender, ImageClickEventArgs e)
        {
            txtSerchWords.Text = "";
            BindData();
        }

        protected void ImgBtnSave_Click(object sender, ImageClickEventArgs e)
        {
            var loginuser = "";
            if (Session["UserID"] != null)
            {
                loginuser = Session["UserID"].ToString();
            }
            var _dal = new AdminUserRoleDal();
            var stu = "";
            if (radbtnYes.Checked)
            {
                stu = "001";
            }
            else
            {
                stu = "002";
            }

            var user = new admin_user()
            {
                userID = txtuserID.Text,
                userName = txtuserName.Text,
                password = txtpassword.Text,
                status = stu,
                createBy = loginuser
            };


            var model = new admin_user_role()
            {
                userRoleID = Guid.NewGuid().ToString().Substring(24, 10),
                userID = txtuserID.Text,
                roleID = ddlRoles.SelectedValue
            };



            // 提交
            if (txtuserID.ReadOnly == false)
            {
                if (_dal.IsUserID(user.userID))
                {

                    if (_dal.InsertUserDate(user))
                    {
                        if (_dal.InsertUserRoleDate(model))
                        {
                            //添加成功
                            ClientScript.RegisterStartupScript(this.GetType(), "提示",
                                "<script type='text/javascript'>alert('添加成功！');</script>");
                            ddlRoles.SelectedValue = model.roleID;
                            BindData();
                        }
                        else
                        {
                            _dal.DelUserDate(user.userID);
                            ClientScript.RegisterStartupScript(this.GetType(), "提示",
                                "<script type='text/javascript'>alert('添加失败！');</script>");
                        }

                    }
                    else
                    {
                        // 添加失败
                        ClientScript.RegisterStartupScript(this.GetType(), "提示",
                            "<script type='text/javascript'>alert('添加失败！');</script>");
                    }
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "提示",
                        "<script type='text/javascript'>alert('用户ID已存在！');</script>");
                }
            }
            else
            {
                if (txtuserID.Text.Length > 0)
                {
                    model.roleID = ddlRoles.SelectedValue;
                    user.updateBy = loginuser;
                    if (_dal.UpdateUserDate(user))
                    {
                        if (_dal.UpdateUserRoleDate(model))
                        {
                            //修改成功
                            ClientScript.RegisterStartupScript(this.GetType(), "提示",
                                "<script type='text/javascript'>alert('修改成功！');</script>");
                            BindData();
                        }

                    }
                    else
                    {
                        // 修改失败
                        ClientScript.RegisterStartupScript(this.GetType(), "提示",
                            "<script type='text/javascript'>alert('修改失败！');</script>");
                    }
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "提示",
                        "<script type='text/javascript'>alert('请选择用户！');</script>");
                }
            }
        }

        protected void ImgBtnAdd_Click(object sender, ImageClickEventArgs e)
        {
            Clie();
        }

        public void Clie()
        {
            txtuserID.Text = "";
            txtuserName.Text = "";
            txtpassword.Text = "";
            txtuserID.ReadOnly = false;
            BindDropDown();
        }

        protected void ImgbtnSelect_Click(object sender, ImageClickEventArgs e)
        {
            BindData();
            Clie();
        }

        protected void ImgBtnDel_Click(object sender, ImageClickEventArgs e)
        {
            if (txtuserID.Text.Length > 0)
            {
                var _dal = new AdminUserRoleDal();
                var roleid = ddlRoles.SelectedValue;

                if (roleid.Length > 0)
                {
                    if (_dal.DelUserDate(txtuserID.Text))
                    {
                        if (_dal.DelUserRoleDate(txtuserID.Text))
                        {
                            //删除成功
                            ClientScript.RegisterStartupScript(this.GetType(), "提示",
                                "<script type='text/javascript'>alert('删除成功！');</script>");
                            Clie();
                            BindData();
                        }

                    }
                    else
                    {
                        // 删除失败
                        ClientScript.RegisterStartupScript(this.GetType(), "提示",
                            "<script type='text/javascript'>alert('删除失败！');</script>");
                    }

                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "提示",
                        "<script type='text/javascript'>alert('请先选择需要删除的角色！');</script>");
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "提示",
                    "<script type='text/javascript'>alert('请选择用户！');</script>");
            }
        }

        protected void tvMenu_SelectedNodeChanged(object sender, EventArgs e)
        {
            var tag = this.tvMenu.SelectedNode.Target;
            if (tag == "角色")
            {
                txtuserID.Text = "";
                txtuserName.Text = "";
                txtpassword.Text = "";
                ddlRoles.SelectedValue = this.tvMenu.SelectedNode.Value;
            }
            else
            {
                var user = this.tvMenu.SelectedNode.Value;
                if (user.Length > 0)
                {
                    var str = " and userid='" + user + "'";
                    var _dal = new AdminUserRoleDal();
                    var alldata = _dal.GetRoleDataBy(str);
                    foreach (var adminUser in alldata)
                    {
                        txtuserName.Text = adminUser.userName;
                        txtuserID.Text = adminUser.userID;
                        txtpassword.Text = adminUser.password;
                        if (adminUser.status == "001")
                        {
                            radbtnYes.Checked = true;
                        }
                        else
                        {
                            radbtnNo.Checked = true;
                        }

                        txtuserID.ReadOnly = true;
                    }
                }
            }

            

        }


        protected void ImgBtnResetP_Click(object sender, ImageClickEventArgs e)
        {
            //
            if (txtuserID.Text.Length > 0)
            {
                var _dal = new AdminUserRoleDal();
                var userid = txtuserID.Text;
                if (_dal.ResetPassword(userid))
                {
                    //添加成功
                    ClientScript.RegisterStartupScript(this.GetType(), "提示",
                        "<script type='text/javascript'>alert('重置成功！');</script>");
                   // BindData();
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "提示",
                        "<script type='text/javascript'>alert('重置失败！');</script>");
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "提示",
                    "<script type='text/javascript'>alert('请选择用户！');</script>");
            }
        }

    }
}